package com.structure;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class StockPrice {
	Map<Integer,Integer> stockPrice;
	TreeMap<Integer,Integer> priceStock;	
	int lastest;

    public StockPrice() {
        stockPrice  = new HashMap<>();
        priceStock = new TreeMap<>();
        lastest = 0;
    }
    
    public void update(int timestamp, int price) {
    	lastest = Math.max(lastest, timestamp);
    	int key = stockPrice.getOrDefault(timestamp, 0);
        stockPrice.put(timestamp, price);
        if(key > 0) {
        	priceStock.put(key,priceStock.get(key) - 1);
        	if(priceStock.get(key) == 0)
        		priceStock.remove(key);
        }
        priceStock.put(price, priceStock.getOrDefault(price, 0)+1);
    }
    
    public int current() {
    	return stockPrice.get(lastest);
    }
    
    public int maximum() {
    	return priceStock.firstKey();
    }
    
    public int minimum() {
    	return priceStock.lastKey();
    }
}
